/*
 * NestedSmartPointer.h
 *
 *  Created on: Oct 11, 2013
 *      Author: Administrator
 */

#ifndef NESTEDSMARTPOINTER_H_
#define NESTEDSMARTPOINTER_H_
#include<iostream>
#include<vector>
using namespace std;
class NestedSmartPointer {
public:
	NestedSmartPointer();
	virtual ~NestedSmartPointer();
};

class Obj{
	static int i,j;
public:
	void f() {cout<<i++<<endl;}
	void g() {cout<<j++<<endl;}
};

int Obj::i = 47;
int Obj::j = 11;

class ObjContainer{
	vector<Obj*> a;
public:
	void add(Obj* obj){a.push_back(obj);}
	class SmartPointer;
	friend SmartPointer;
	class SmartPointer{
		ObjContainer& oc;
		unsigned int index;
		int ptr;
	public:
		SmartPointer(ObjContainer& objc, int ptr): oc(objc){
			index =0;
			this->ptr = ptr;
		}
		//Return value indicates end of list:
		bool operator++(){
			//prefix
			if(index>= oc.a.size()) return false;
			if(oc.a[++index] == 0) return false;
			return true;
		}
		bool operator++(int){
			//postfix
			return operator++();
		}

		bool operator --(){
			//prefix
			if(index == 0 ) return false;
			index--;
			return true;
		}
		bool operator --(int){
			//postfix
			return operator --();
		}

		Obj* operator->() const{
			return oc.a[index];
		}

		bool output1()
		{
			cout <<"output1"<<endl;
			return true;
		}

		bool output2()
		{
			cout<<"output2"<<endl;
			return true;
		}
		bool operator() (){
			if(ptr==1) return output1();
			if(ptr==2) return output2();
			return false;
		}

	};
	SmartPointer begin(){
		return SmartPointer(*this,-1);
	}

	SmartPointer operator->*(int ptr)
	{
		return SmartPointer(*this,ptr);
	}

};
#endif /* NESTEDSMARTPOINTER_H_ */
